package com.esun.payment_api.service.impl;

import com.esun.common.utils.PinyinUtils;
import com.esun.payment_api.bean.WldwBean;
import com.esun.payment_api.domain.SysCustomer;
import com.esun.payment_api.mapper.SysCustomerMapper;
import com.esun.payment_api.service.SysCustomerService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("SysCustomerService")
public class SysCustomerServiceImpl implements SysCustomerService {

    @Resource
    SysCustomerMapper sysCustomerMapper;

    @Override
    public SysCustomer selectById(Integer id) {
        return sysCustomerMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteById(Integer id) {
        return sysCustomerMapper.deleteById(id);
    }

    @Override
    public int updateById(SysCustomer record) {
        return sysCustomerMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<WldwBean> getWldwList(String searchStr, Integer coId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum ,pageSize);
        List<WldwBean> list = sysCustomerMapper.getWldwList(searchStr, coId);
        if (list != null && list.size() >0){
            //Comparator com = Collator.getInstance(Locale.CHINA);
            //按字母排序
            // Collections.sort(list.stream().map(WldwList::getName).collect(Collectors.toList()), com);
            //循环找出首字母一样的数据
            for (WldwBean e:list){
                try {
                    String name = e.getName();
                    System.out.println("单位名：" + name);
                    name = name.replaceAll("《", "<");
                    name = name.replaceAll("》", ">");
                    name = name.replaceAll("（", "(");
                    name = name.replaceAll("）", ")");
                    String zm = PinyinUtils.getFullSpell(name).substring(0,1);
                    Character character = zm.charAt(0);
                    System.out.println(character);
                    if((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z')){
                        Character firstCharacter = name.substring(0,1).charAt(0);
                        if((firstCharacter >= 'a' && firstCharacter <= 'z') || (firstCharacter >= 'A' && firstCharacter <= 'Z')){
                            e.setInitials("#");
                        } else {
                            e.setInitials(zm);
                        }
                    } else {
                        e.setInitials("#");
                    }
                }catch (Exception exception){
                    e.setInitials("#");
                }
            }
        }
        return list;
    }
}
